package admin.member;
import java.util.ArrayList;
import java.util.List;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.BaseUtil;
import comm.IbatisLoader;
import comm.ibatisDefault;

import org.apache.log4j.Logger;

public class MemberDao extends ibatisDefault{
	protected final Logger logger = Logger.getLogger(getClass());
	
	private IbatisLoader 	iBatis;
	private SqlMapClient 	sqlMapper;
	
	public MemberDao(){
		this.sqlMapper = iBatis.setConfig();
	}
	
	/**
	 * �쉶�썝 由ъ뒪�듃 硫붿냼�뱶
	 * @param paramMap (searchGubn,searchValue)
	 * @return ArrayList
	 */
	public ArrayList getList(HashMap paramMap){
		ArrayList returnList = new ArrayList();
		try{
			/**
			 * �럹�씠吏� 泥섎━ �떆�옉
			 */
			int page = Integer.parseInt((String)paramMap.get("giCurPage")); // �쁽�젣 �럹�씠吏� 踰덊샇
			if (page == 0) page = 1;
			int pageSize = Integer.parseInt((String)paramMap.get("pageSize")); //�븳�럹�씠吏��떦 蹂댁뿬吏� 寃뚯떆臾쇱쓽 媛� �닔
			if (pageSize == 0) pageSize = 10;
			int startPos = ((page-1)*pageSize);			
			
			paramMap.put("START",startPos);
			paramMap.put("STOP",pageSize);
			paramMap.put("memberGubn",(String)paramMap.get("memberGubn"));
			paramMap.put("searchGubn",(String)paramMap.get("searchGubn"));
			paramMap.put("searchValue",(String)paramMap.get("searchValue"));			
			
			cnt = ((Integer)sqlMapper.queryForObject("member.member_cnt",paramMap)).intValue();	
			returnList = (ArrayList)sqlMapper.queryForList("member.member_List",paramMap);
			
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnList;
	}
	
	/**
	 * �쉶�썝 �긽�꽭 �럹�씠吏�
	 * @param paramMap (idx)
	 * @return ArrayList
	 */
	public ArrayList getView(HashMap paramMap){
		ArrayList returnList = new ArrayList();
		try{
			returnList = (ArrayList)sqlMapper.queryForList("member.member_view",paramMap);
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnList;
	}	

	/**
	 * �벑濡� �닔�젙�떆 �뱾�뼱 �삤�뒗 寃껋쓣 遺꾧린 �빐二쇰뒗 怨�
	 * @param request
	 * @return
	 */
	public HashMap bunki(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			boolean retValue = false; //�벑濡�, �닔�젙 �썑 由ы꽩媛�
			//�쁽�옱 �뱾�뼱 �삤�뒗 怨녹씠 �벑濡�,�닔�젙�씤吏� 援щ텇
			String mode = BaseUtil.getParame(request.getParameter("mode"),"");
			if(mode.equals("I")){// �벑濡앹씪 寃쎌슦
				retValue = getInsert(request);
			}else if(mode.equals("U")){// �닔�젙�씪 寃쎌슦
				/**
				 * �럹�씠吏� 湲곕낯媛믪쓣 �뀑�똿 �빐以��떎.
				 */
				returnValue.put("id",				BaseUtil.getParame(request.getParameter("id"),""));
				returnValue.put("searchGubn",		BaseUtil.getParame(request.getParameter("searchGubn"),""));
				returnValue.put("searchValue",		BaseUtil.getParame(request.getParameter("searchValue"),""));
				returnValue.put("txtSelectPage",	BaseUtil.getParame(request.getParameter("txtSelectPage"),""));
				returnValue.put("txtSelectBlock",	BaseUtil.getParame(request.getParameter("txtSelectBlock"),""));
				retValue = getUpdate(request);
			}
			returnValue.put("mode" , mode);
			returnValue.put("saveYn" , retValue+"");
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	/**
	 * �쉶�썝 �벑濡앹씪 寃쎌슦
	 * @param multi
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean getInsert(HttpServletRequest request){
		boolean returnValue = false;
		try{
			System.out.println("aaaaaaaaa");
			HashMap paramMap = new HashMap();
			String	tel	= request.getParameter("tel1")+request.getParameter("tel2")+request.getParameter("tel3");
			String	hp	= request.getParameter("hp1")+request.getParameter("hp2")+request.getParameter("hp3");
			String	jumin	= request.getParameter("jumin1")+request.getParameter("jumin2");			
			String	email = "";
			String  zipcode = request.getParameter("zipcode1")+request.getParameter("zipcode2");
			if(request.getParameter("email2").equals("")){
				email = request.getParameter("email1")+"@"+request.getParameter("email3");
			}else{
				email = request.getParameter("email1")+"@"+request.getParameter("email2");
			}
			System.out.println("bbbbbbbbbbb");
			paramMap.put("id"				, BaseUtil.getParame(request.getParameter("hiddenid"),""));
			paramMap.put("name"				, BaseUtil.getParame(request.getParameter("name"),""));
			paramMap.put("jumin"			, BaseUtil.getParame(jumin,""));
			paramMap.put("pwd"				, BaseUtil.getParame(request.getParameter("pwd"),""));
			paramMap.put("tel"				, BaseUtil.getParame(tel,""));
			paramMap.put("hp"				, BaseUtil.getParame(hp,""));
			paramMap.put("email"			, BaseUtil.getParame(email,""));
			paramMap.put("grade"			, BaseUtil.getParame(request.getParameter("grade"),""));
			paramMap.put("authorize"		, BaseUtil.getParame(request.getParameter("authorize"),""));
			paramMap.put("sex"		, BaseUtil.getParame(request.getParameter("sex"),""));
			paramMap.put("wedding_yn"		, BaseUtil.getParame(request.getParameter("wedding_yn"),""));
			paramMap.put("academic"		, BaseUtil.getParame(request.getParameter("academic"),""));
			paramMap.put("advice_date"		, BaseUtil.getParame(request.getParameter("advice_date"),""));
			paramMap.put("advice_time"		, BaseUtil.getParame(request.getParameter("advice_time"),""));
			paramMap.put("zipcode"			, BaseUtil.getParame(zipcode,""));
			paramMap.put("addr1"			, BaseUtil.getParame(request.getParameter("addr1"),""));
			paramMap.put("addr2"			, BaseUtil.getParame(request.getParameter("addr2"),""));
			if(sqlMapper.update("member.member_insert", paramMap) > 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}
	
	/**
	 * �쉶�썝 �닔�젙�씪 寃쎌슦
	 * @param multi
	 * @return
	 */
	public boolean getUpdate(HttpServletRequest request){
		boolean returnValue = false;
		try{
			
			HashMap paramMap = new HashMap();
			String	tel	= request.getParameter("tel1")+request.getParameter("tel2")+request.getParameter("tel3");
			String	hp	= request.getParameter("hp1")+request.getParameter("hp2")+request.getParameter("hp3");
			String  zipcode = request.getParameter("zipcode1")+request.getParameter("zipcode2");
			String	email = "";
			if(request.getParameter("email2").equals("")){
				email = request.getParameter("email1")+"@"+request.getParameter("email3");
			}else{
				email = request.getParameter("email1")+"@"+request.getParameter("email2");
			}
			paramMap.put("pwd"				, BaseUtil.getParame(request.getParameter("pwd"),""));
			paramMap.put("tel"				, "");
			paramMap.put("hp"				, BaseUtil.getParame(hp,""));
			paramMap.put("email"			, BaseUtil.getParame(email,""));
			paramMap.put("grade"			, BaseUtil.getParame(request.getParameter("grade"),""));
			paramMap.put("id"				, BaseUtil.getParame(request.getParameter("id"),""));
			paramMap.put("sex"				, BaseUtil.getParame(request.getParameter("sex"),""));
			paramMap.put("wedding_yn"		, BaseUtil.getParame(request.getParameter("wedding_yn"),""));
			paramMap.put("academic"			, BaseUtil.getParame(request.getParameter("academic"),""));
			paramMap.put("advice_date"		, BaseUtil.getParame(request.getParameter("advice_date"),""));
			paramMap.put("advice_time"		, BaseUtil.getParame(request.getParameter("advice_time"),""));
			paramMap.put("zipcode"			, BaseUtil.getParame(zipcode,""));
			paramMap.put("addr1"			, BaseUtil.getParame(request.getParameter("addr1"),""));
			paramMap.put("addr2"			, BaseUtil.getParame(request.getParameter("addr2"),""));
			
			

			if(sqlMapper.update("member.member_update", paramMap)> 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	/**
	 * �쉶�썝 �궘�젣
	 * @param request
	 * @return
	 */
	public HashMap getDelete(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("idx",	BaseUtil.getParame(request.getParameter("idx"),""));
			
			String	mode = BaseUtil.getParame(request.getParameter("mode"),"D");
			
			returnValue.put("idx", BaseUtil.getParame(request.getParameter("idx"),""));
			returnValue.put("searchGubn", BaseUtil.getParame(request.getParameter("searchGubn"),""));
			returnValue.put("searchValue", BaseUtil.getParame(request.getParameter("searchValue"),""));
			returnValue.put("txtSelectPage", BaseUtil.getParame(request.getParameter("txtSelectPage"),"0"));
			returnValue.put("txtSelectBlock", BaseUtil.getParame(request.getParameter("txtSelectBlock"),"0"));
			
			if(sqlMapper.update("member.member_delete", paramMap)> 0){
				returnValue.put("saveYn","true");
				returnValue.put("mode" , mode);
			}else{
				returnValue.put("saveYn","false");
				returnValue.put("mode" , mode);
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	public HashMap getIdcheck(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("id",	BaseUtil.getParame(request.getParameter("id"),""));
			cnt = ((Integer)sqlMapper.queryForObject("member.member_check",paramMap)).intValue();
			if(cnt <= 0){
				returnValue.put("saveYn","true");
				returnValue.put("id",request.getParameter("id"));
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	
	/**
	 * �븘�씠�뵒/鍮꾨��踰덊샇 李얘린
	 * @param multi
	 * @return
	 */
	public HashMap getFind(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			//�쁽�옱 �뱾�뼱 �삤�뒗 怨녹씠 �벑濡�,�닔�젙�씤吏� 援щ텇
			String mode = BaseUtil.getParame(request.getParameter("mode"),"");
			if(mode.equals("I")){
				returnValue = getFindId(request);
			}else if(mode.equals("P")){
				returnValue = getFindPwd(request);
			}
			returnValue.put("mode" , mode);
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	public HashMap getFindId(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			String jumin = request.getParameter("jumin1") + request.getParameter("jumin2");
			paramMap.put("name",	BaseUtil.getParame(request.getParameter("name"),""));
			paramMap.put("jumin",	BaseUtil.getParame(jumin,""));
			String id = (String)sqlMapper.queryForList("member.member_find_id",paramMap).toString();
			if(!id.equals("[]")){
				returnValue.put("saveYn","true");
				returnValue.put("id",id);
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	public HashMap getFindPwd(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			String jumin = request.getParameter("pjumin1") + request.getParameter("pjumin2");
			paramMap.put("id",	BaseUtil.getParame(request.getParameter("id"),""));
			paramMap.put("name",	BaseUtil.getParame(request.getParameter("pname"),""));
			paramMap.put("jumin",	BaseUtil.getParame(jumin,""));
			String pwd = (String)sqlMapper.queryForList("member.member_find_pwd",paramMap).toString();
			if(!pwd.equals("[]")){
				returnValue.put("saveYn","true");
				returnValue.put("pwd",pwd);
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	public List loginOk(String login_id ,String login_pw){
		List returnValue = new ArrayList();
		HashMap map = new HashMap();
		map.put("id",login_id);
		map.put("pwd",login_pw);
		try {
			returnValue = (List)sqlMapper.queryForList("member.loginOk",map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnValue;
	}	
	
	public HashMap bunki2(HttpServletRequest request){
		HashMap returnValue = new HashMap();
		try{
			boolean retValue = false; //�벑濡�, �닔�젙 �썑 由ы꽩媛�
			//�쁽�옱 �뱾�뼱 �삤�뒗 怨녹씠 �벑濡�,�닔�젙�씤吏� 援щ텇
			String mode = BaseUtil.getParame(request.getParameter("mode"),"");
			if(mode.equals("I")){// �벑濡앹씪 寃쎌슦
				retValue = getInsert2(request);
			}else if(mode.equals("U")){// �닔�젙�씪 寃쎌슦
				
				
				
				returnValue.put("id",				BaseUtil.getParame(request.getParameter("id"),""));
				returnValue.put("searchGubn",		BaseUtil.getParame(request.getParameter("searchGubn"),""));
				returnValue.put("searchValue",		BaseUtil.getParame(request.getParameter("searchValue"),""));
				returnValue.put("txtSelectPage",	BaseUtil.getParame(request.getParameter("txtSelectPage"),""));
				returnValue.put("txtSelectBlock",	BaseUtil.getParame(request.getParameter("txtSelectBlock"),""));
				retValue = getUpdate2(request);
			}
			returnValue.put("mode" , mode);
			returnValue.put("saveYn" , retValue+"");
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	/***
	 * 사용자단 회원가입
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean getInsert2(HttpServletRequest request){
		boolean returnValue = false;
		try{
			System.out.println("aaaaaaaaa");
			HashMap paramMap = new HashMap();
			String	tel			= "";
			String	hp			= request.getParameter("hp1")+request.getParameter("hp2")+request.getParameter("hp3");
			String	birthday	= request.getParameter("birthday1")+request.getParameter("birthday2")+request.getParameter("birthday3");			
			String	email 		= request.getParameter("email");
			String  zipcode 	= request.getParameter("zipcode1")+request.getParameter("zipcode2");
			
			System.out.println("bbbbbbbbbbb");
			paramMap.put("id"				, BaseUtil.getParame(request.getParameter("hiddenid"),""));
			paramMap.put("name"				, BaseUtil.getParame(request.getParameter("name"),""));
			paramMap.put("jumin"			, BaseUtil.getParame(birthday,""));
			paramMap.put("pwd"				, BaseUtil.getParame(request.getParameter("pwd"),""));
			paramMap.put("tel"				, BaseUtil.getParame(tel,""));
			paramMap.put("hp"				, BaseUtil.getParame(hp,""));
			paramMap.put("email"			, BaseUtil.getParame(email,""));
			paramMap.put("grade"			, BaseUtil.getParame(request.getParameter("grade"),""));
			paramMap.put("authorize"		, BaseUtil.getParame(request.getParameter("authorize"),""));
			paramMap.put("sex"		, BaseUtil.getParame(request.getParameter("sex"),""));
			paramMap.put("wedding_yn"		, BaseUtil.getParame(request.getParameter("wedding_yn"),""));
			paramMap.put("academic"		, BaseUtil.getParame(request.getParameter("academic"),""));
			paramMap.put("advice_date"		, BaseUtil.getParame(request.getParameter("advice_date"),""));
			paramMap.put("advice_time"		, BaseUtil.getParame(request.getParameter("advice_time"),""));
			paramMap.put("zipcode"			, BaseUtil.getParame(zipcode,""));
			paramMap.put("addr1"			, BaseUtil.getParame(request.getParameter("addr1"),""));
			paramMap.put("addr2"			, BaseUtil.getParame(request.getParameter("addr2"),""));
			if(sqlMapper.update("member.member_insert", paramMap) > 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}
	
	public boolean getUpdate2(HttpServletRequest request){
		boolean returnValue = false;
		try{
			
			HashMap paramMap = new HashMap();
			String	tel			= request.getParameter("tel1")+request.getParameter("tel2")+request.getParameter("tel3");
			String	hp			= request.getParameter("hp1")+request.getParameter("hp2")+request.getParameter("hp3");
			String  zipcode 	= request.getParameter("zipcode1")+request.getParameter("zipcode2");			
			String 	birthday 	= request.getParameter("birthday1")+request.getParameter("birthday2")+request.getParameter("birthday3");
			
			System.out.println(birthday);
			paramMap.put("pwd"				, BaseUtil.getParame(request.getParameter("pwd"),""));
			paramMap.put("birthday"			, BaseUtil.getParame(birthday,""));
			paramMap.put("name"				, BaseUtil.getParame(request.getParameter("name"),""));
			paramMap.put("tel"				, "");
			paramMap.put("hp"				, BaseUtil.getParame(hp,""));
			paramMap.put("email"			, BaseUtil.getParame(request.getParameter("email"),""));
			paramMap.put("grade"			, BaseUtil.getParame(request.getParameter("grade"),""));
			paramMap.put("id"				, BaseUtil.getParame(request.getParameter("id"),""));
			paramMap.put("sex"				, BaseUtil.getParame(request.getParameter("sex"),""));
			paramMap.put("wedding_yn"		, BaseUtil.getParame(request.getParameter("wedding_yn"),""));
			paramMap.put("academic"			, BaseUtil.getParame(request.getParameter("academic"),""));
			paramMap.put("advice_date"		, BaseUtil.getParame(request.getParameter("advice_date"),""));
			paramMap.put("advice_time"		, BaseUtil.getParame(request.getParameter("advice_time"),""));
			paramMap.put("zipcode"			, BaseUtil.getParame(zipcode,""));
			paramMap.put("addr1"			, BaseUtil.getParame(request.getParameter("addr1"),""));
			paramMap.put("addr2"			, BaseUtil.getParame(request.getParameter("addr2"),""));
			
			

			if(sqlMapper.update("member.member_update2", paramMap)> 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	public HashMap getDelete2(Integer idx){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			paramMap.put("idx",	idx);			
			returnValue.put("idx", idx);			
			
			if(sqlMapper.update("member.member_delete", paramMap)> 0){
				returnValue.put("saveYn","true");
				
			}else{
				returnValue.put("saveYn","false");
				
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}		
	
	public HashMap getFindId2(String name,String hp){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			
			paramMap.put("name",	BaseUtil.getParame(name,""));
			paramMap.put("hp",	BaseUtil.getParame(hp,""));
			String id = (String)sqlMapper.queryForList("member.member_find_id",paramMap).toString();
			if(!id.equals("[]")){
				returnValue.put("saveYn","true");
				returnValue.put("id",id);
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}	
	
	public HashMap getFindPwd2(String id, String name,String hp){
		HashMap returnValue = new HashMap();
		try{
			HashMap paramMap = new HashMap();
			
			paramMap.put("id",	BaseUtil.getParame(id,""));
			paramMap.put("name",	BaseUtil.getParame(name,""));
			paramMap.put("hp",	BaseUtil.getParame(hp,""));
			String pwd = (String)sqlMapper.queryForList("member.member_find_pwd",paramMap).toString();
			if(!pwd.equals("[]")){
				returnValue.put("saveYn","true");
				returnValue.put("pwd",pwd);
			}else{
				returnValue.put("saveYn","false");
			}
		}catch(Exception sqlEx){
			logger.error(sqlEx);
		}
		return returnValue;
	}
}
